{include file="admin@public/header"/}

<link href="/public/static/admin/css/authority.css" rel="stylesheet" type="text/css">
<style>
    .forNew {
        font-size: 14px;
        font-weight: bold;
        color: #000000;
    }

    .forNew a {
        background: linear-gradient(to bottom, #48BFEA, #16ABE0);
        ;
        color: #fff;
        text-decoration: none;
        border-radius: 3px;
        font-weight: normal;
        padding: 6px 18px;
        margin-left: 5px;
    }

    .forNew li {
        line-height: 30px;
    }

    .dataManage {
        font-size: 12px;
        color: #636363;
    }

    .edit-box {
        width: 100%;
        height: 100%;
        text-align: center;
        position: relative;
    }

    .edit-btn {
        display: inline-block;
        text-decoration: none;
        border: 1px solid #ccc;
        border-radius: 12px;
        width: 55px;
        height: 25px;
        background-color: #fff;
        vertical-align: middle;
        text-align: center;
        margin-top: 8px;
    }

    .webManage {
        padding: 7px 0;
    }

    .edit-icon {
        display: inline-block;
        background: url(../css/img/icon_sicheck.png);
        width: 30px;
        height: 30px;
        position: absolute;
        right: -5px;
    }

    .tab-bar {
        margin: 20px 0 20px 20px;
        color: black;
        height: 30px;
        font-size: 14px;
        border-bottom: 1px solid #ccc;
    }

    .tab-item {
        float: left;
        padding: 5px 15px;
        line-height: 20px;
    }

    .tab-item-other {
        float: left;
        padding: 5px 15px;
        line-height: 20px;
        cursor: pointer;
    }

    .tab-item-other a {
        margin-right: 5px;
    }

    .tab-item.active {
        color: #3cbaff;
        border: 1px solid #ccc;
        border-bottom: none;
        position: relative;
        border-radius: 4px 4px 0 0;
    }

    .tab-item.active::after {
        position: absolute;
        content: '';
        height: 3px;
        width: 100%;
        bottom: -1px;
        left: 0;
        background-color: #f5f5f5;
    }

    .content {
        margin: 20px 0 20px 20px;
    }

    .tab-content {
        display: none;
    }

    .tab-content.active {
        display: block;
    }

    .content-wrap {
        width: 1120px;
    }

    .content-wrap.rights-box {
        background-color: #fff;
    }

    .left-wrap {
        width: 200px;
        float: left;
    }

    .right-wrap {
        margin-left: 200px;
        border-left: 1px solid #ddd;
    }

    .tree-wrap {
        width: 180px;
        background-color: #fff;
    }

    .staff {
        width: 1120px;
    }

    .staff-operate {
        height: 32px;
        margin-bottom: 20px;
    }

    .mr-10 {
        margin-right: 10px;
    }

    .tree-node {
        height: 28px;
    }

    .tree-node-selected {
        background-color: #D4EFF8;
        border: 1px solid #00b9ef;
        color: #666;
        padding: 0;
        box-sizing: border-box;
    }

    .tree-wrap .easyui-tree#authority_tt li .tree-node span.button.hoverAdd {
        display: none !important;
    }

    .tree-wrap .easyui-tree#authority_tt li .tree-node-hover span.button.hoverAdd {
        display: inline-block !important;
    }

    .tree-wrap .easyui-tree#authority_tt li .tree-node-selected span.button.hoverAdd {
        display: inline-block !important;
    }

    .tipBox {
        width: 100%;
        height: auto;
        display: flex;
        align-items: center;
        padding: 0 20px;
        box-sizing: border-box;
    }

    .tipBox .leftImg {
        flex: none;
        width: 48px;
        height: 48px;
    }

    .tipBox .rightText {
        flex: 1;
        width: calc(100% - 48px);
        padding: 20px;
        box-sizing: border-box;
        font-size: 14px;
    }

    .tree-wrap .easyui-tree#authority_tt li .tree-node {
        overflow: hidden;
    }

    .tree-wrap .easyui-tree#authority_tt li .textTitle {
        display: inline-block;
        width: auto;
        overflow: hidden;
    }

    .tree-wrap .easyui-tree#authority_tt li .textInputTitle {
        display: none;
        width: 100px;
        overflow: hidden;
        height: 100%;
        outline: none;
    }

    .layui-form-checkbox span {
        font-size: 12px;
    }

    .rights .col-name {
        display: inline-flex;
        align-items: center;
    }

    .rights .col-name .layui-form-checkbox[lay-skin=primary] {
        float: left;
    }

    .rights-title .fold-btn {
        margin-left: 0;
        margin-top: 0;
    }

    .layui-form-checked[lay-skin=primary] i {
        border-color: #35b9ee;
        background-color: #cff1fc;
        color: #35b9ee;
        font-weight: bolder;
    }

    .layui-form-checked[lay-skin=primary]:hover i {
        border-color: #35b9ee;
        color: #35b9ee;
    }

    .layui-form-checkbox[lay-skin=primary]:hover i {
        border-color: #35b9ee;
    }

    .layui-form-checkbox[lay-skin=primary] {
        padding-left: 20px;
    }

    .layui-form-checkbox[lay-skin=primary] span {
        padding-right: 5px;
        display: block;
        overflow: hidden;
    }

    .others li {
        margin-right: 10px;
    }
</style>

<div class="tab-bar">
    <div class="tab-item active" data-id="staff">新增同事</div>
    <div class="tab-item" data-id="role">角色管理</div>
    <div class="tab-item" data-id="authority">数据授权</div>
    <!-- <div class="tab-item-other" id="openUrl"><a href="https://club.kingdee.com/club/newclub/school/course?product_id=7&amp;cid=1546#pid=5668" target="_blank" class="video-icon"></a>权限设置</div> -->
</div>

<div class="content">

    <div class="tab-content active">
        <div class="staff">
            <div class="staff-operate clearfix">
                <a class="btn fr actor" id="actor">邀请外部协作者</a>
                <a class="btn fr mr-10 invite" id="invite" onclick="lopen('add.html','新增', '400px', '360px', 'saveuser.html')">添加管理员</a> 
            </div>
            <div class="grid-wrap">
                <table id="staffList"></table>
            </div>
        </div>
    </div>

    <div class="tab-content clearfix ">
        <div class="content-wrap rights-box layui-form">
            <div class="left-wrap" style="overflow: hidden;">
                <div class="tree-wrap">
                    <ul class="easyui-tree" id="authority_tt">

                    </ul>
                </div>
            </div>
            <div class="right-wrap">
                <ul class="menu clearfix" style="width: 100%;position: relative;">
                    <!-- <li class="menu-item" data-id="APP">精斗云APP</li> -->
                    <li class="menu-item cur" data-id="JXC" id="menu-jxc"><span class="fl">云进销存</span><span
                            class="question fl" title="仅适用于“云进销存”产品授权"></span></li>
                </ul>
                <div class="btn-wrap clearfix" id="savewrap">
                    <a class="btn fr" id="saveRight">保存</a>
                </div>


                <div class="rights-wrap" id="rights-wrap">
                    <ul class="rights for-common">
                        <!-- 默认数据（新账套进来都是空的） -->
                        <li class="rights-header">
                            <ul class="rights-title clearfix">
                                <li class="checkbox-wrap col-name">
                                    <input type="checkbox" name="name" id="allChoose" lay-skin="primary" title="全部"
                                        data-type="name" lay-filter="allChoose" data-ids="nameAll" data-text="one">
                                </li>
                                <li class="checkbox-wrap col-normal">
                                    <input type="checkbox" name="query" id="queryAllChoose" lay-skin="primary"
                                        title="查询" data-type="query" lay-filter="typeAllChoose" data-ids="queryAll"
                                        data-text="one">
                                </li>
                                <li class="checkbox-wrap col-normal">
                                    <input type="checkbox" name="add" id="addAllChoose" lay-skin="primary" title="新增"
                                        data-type="add" lay-filter="typeAllChoose" data-ids="addAll" data-text="one">
                                </li>
                                <li class="checkbox-wrap col-normal">
                                    <input type="checkbox" name="update" id="updateAllChoose" lay-skin="primary"
                                        title="修改" data-type="update" lay-filter="typeAllChoose" data-ids="updateAll"
                                        data-text="one">
                                </li>
                                <li class="checkbox-wrap col-normal">
                                    <input type="checkbox" name="delete" id="deleteAllChoose" lay-skin="primary"
                                        title="删除" data-type="delete" lay-filter="typeAllChoose" data-ids="deleteAll"
                                        data-text="one">
                                </li>
                                <li class="checkbox-wrap col-normal">
                                    <input type="checkbox" name="check" id="checkAllChoose" lay-skin="primary"
                                        title="审核" data-type="check" lay-filter="typeAllChoose" data-ids="checkAll"
                                        data-text="one">
                                </li>
                                <li class="checkbox-wrap col-normal">
                                    <input type="checkbox" name="uncheck" id="uncheckAllChoose" lay-skin="primary"
                                        title="反审核" data-type="uncheck" lay-filter="typeAllChoose" data-ids="uncheckAll"
                                        data-text="one">
                                </li>
                                <li class="checkbox-wrap col-normal">
                                    <input type="checkbox" name="import" id="importAllChoose" lay-skin="primary"
                                        title="导入" data-type="import" lay-filter="typeAllChoose" data-ids="importAll"
                                        data-text="one">
                                </li>
                                <li class="checkbox-wrap col-normal">
                                    <input type="checkbox" name="export" id="exportAllChoose" lay-skin="primary"
                                        title="导出" data-type="export" lay-filter="typeAllChoose" data-ids="exportAll"
                                        data-text="one">
                                </li>
                                <li class="checkbox-wrap col-others">
                                    <input type="checkbox" name="others" id="othersAllChoose" lay-skin="primary"
                                        title="其它" data-type="others" lay-filter="typeAllChoose" data-ids="othersAll"
                                        data-text="one">
                                </li>
                            </ul>
                        </li>
                    </ul>
                    <!-- 固定范围滚动区块 -->
                    <ul class="rights for-common-roll clearfix">
                        {volist name="menu", id="vo"}
                        <li class="rights-model clearfix">
                            <ul class="rights-title clearfix " data-model="{$vo.name}">
                                <li class="col-name checkbox-wrap">
                                    <input type="checkbox" name="name" id="" lay-skin="primary" title="{$vo.name}"
                                        data-type="name" data-paid="{$vo.id}" lay-filter="childAllChoose">
                                    <span class="fold-btn fl fold-close"></span>
                                </li>
                                <li class="checkbox-wrap col-normal">
                                    <input type="checkbox" name="query" id="" data-ids="query{$vo.id}" data-text="one"
                                        lay-skin="primary" title="" data-type="query" lay-filter="childAll">
                                </li>
                                <li class="checkbox-wrap col-normal">
                                    <input type="checkbox" name="add" id="" data-ids="add{$vo.id}" data-text="one"
                                        lay-skin="primary" title="" data-type="add" lay-filter="childAll">
                                </li>
                                <li class="checkbox-wrap col-normal">
                                    <input type="checkbox" name="update" id="" data-ids="update{$vo.id}" data-text="one"
                                        lay-skin="primary" title="" data-type="update" lay-filter="childAll">
                                </li>
                                <li class="checkbox-wrap col-normal">
                                    <input type="checkbox" name="delete" id="" data-ids="delete{$vo.id}" data-text="one"
                                        lay-skin="primary" title="" data-type="delete" lay-filter="childAll">
                                </li>
                                <li class="checkbox-wrap col-normal">
                                    <input type="checkbox" name="check" id="" data-ids="check{$vo.id}" data-text="one"
                                        lay-skin="primary" title="" data-type="check" lay-filter="childAll">
                                </li>
                                <li class="checkbox-wrap col-normal">
                                    <input type="checkbox" name="uncheck" id="" data-ids="uncheck{$vo.id}"
                                        data-text="one" lay-skin="primary" title="" data-type="uncheck"
                                        lay-filter="childAll">
                                </li>
                                <li class="checkbox-wrap col-normal">
                                    <input type="checkbox" name="import" id="" data-ids="import{$vo.id}" data-text="one"
                                        lay-skin="primary" title="" data-type="import" lay-filter="childAll">
                                </li>
                                <li class="checkbox-wrap col-normal">
                                    <input type="checkbox" name="export" id="" data-ids="export{$vo.id}" data-text="one"
                                        lay-skin="primary" title="" data-type="export" lay-filter="childAll">
                                </li>
                                <li class="checkbox-wrap col-normal">
                                    <input type="checkbox" name="others" id="" data-ids="others{$vo.id}" data-text="one"
                                        lay-skin="primary" title="" data-type="others" lay-filter="childAll">
                                </li>
                            </ul>
                            {volist name="vo.children" id="vv"}
                            <ul class="rights-content clearfix hide" data-model="{$vo.name}" data-object="37">
                                <li class="col-name checkbox-wrap">
                                    <input type="checkbox" name="name" id="" data-id="{$vv.id}" data-pid="{$vv.id}"
                                        lay-skin="primary" title="{$vv.name}" data-type="name"
                                        lay-filter="grandsonAllChoose">
                                </li>
                                <li class="col-normal checkbox-wrap">
                                    <input type="checkbox" name="query" id="" lay-skin="primary" title=""
                                        data-type="query" data-id="1" data-pid="{$vv.id}" lay-filter="itemBox">
                                </li>
                                <li class="col-normal checkbox-wrap">
                                    <input type="checkbox" name="add" id="" lay-skin="primary" title="" data-type="add"
                                        data-id="2" data-pid="{$vv.id}" lay-filter="itemBox">
                                </li>
                                <li class="col-normal checkbox-wrap">
                                    <input type="checkbox" name="update" id="" lay-skin="primary" title=""
                                        data-type="update" data-id="3" data-pid="{$vv.id}" lay-filter="itemBox">
                                </li>
                                <li class="col-normal checkbox-wrap">
                                    <input type="checkbox" name="delete" id="" lay-skin="primary" title=""
                                        data-type="delete" data-id="4" data-pid="{$vv.id}" lay-filter="itemBox">
                                </li>
                                <li class="col-normal checkbox-wrap">
                                    <input type="checkbox" name="check" id="" lay-skin="primary" title=""
                                        data-type="check" data-id="5" data-pid="{$vv.id}" lay-filter="itemBox">
                                </li>
                                <li class="col-normal checkbox-wrap">
                                    <input type="checkbox" name="uncheck" id="" lay-skin="primary" title=""
                                        data-type="uncheck" data-id="6" data-pid="{$vv.id}" lay-filter="itemBox">
                                </li>
                                <li class="col-normal checkbox-wrap">
                                    <input type="checkbox" name="import" id="" lay-skin="primary" title=""
                                        data-type="import" data-id="7" data-pid="{$vv.id}" lay-filter="itemBox">
                                </li>
                                <li class="col-normal checkbox-wrap">
                                    <input type="checkbox" name="export" id="" lay-skin="primary" title=""
                                        data-type="export" data-id="8" data-pid="{$vv.id}" lay-filter="itemBox">
                                </li>
                                <li class="col-normal checkbox-wrap">
                                    <input type="checkbox" name="others" id="" lay-skin="primary" title=""
                                        data-type="others" data-id="9" data-pid="{$vv.id}" lay-filter="itemBox">
                                </li>
                                <!-- <li class="col-others">
                                    <ul class="others clearfix">
                                        <li class="checkbox-wrap">
                                            <input type="checkbox" name="others" id="" lay-skin="primary" title=""
                                                data-type="others" data-id="3080" lay-filter="itemBox">
                                        </li>
                                    </ul>
                                </li> -->
                            </ul>
                            {/volist}
                        </li>
                        {/volist}
                    </ul>
                </div>
            </div>
        </div>
    </div>

    <div class="tab-content">
        <div class="grid-wrap">
            <table id="grid"></table>
        </div>
    </div>

</div>


<!--  以隐藏的方式获取下拉的样式 ，作用是样式 -->
<div id="p_role_id" style="display: none"></div>
<!--  以隐藏的方式获取角色的样式 ，作用是赋值 -->
<div style="display:none;">
    <input type="text" id="role_name" value="" autocomplete="off" readonly="">
</div>

<!-- 角色弹窗(组合件) -->
<div class="ui-droplist-wrap" id="role_wrap" style="position: absolute; z-index: 1000; width: 215px;display:none;">
    <div class="droplist" id="role_list" style="position: relative; overflow: auto; min-height: 150px; height: 150px;">
        {volist name="role" id="vo"}
        <div class="list-item" role_id="{$vo.id}" title="{$vo.name}">{$vo.name}</div>
        {/volist}
    </div>
</div>



<script type="text/javascript" src="/public/static/admin/easyui/jeasyui/jquery.easyui.min.js"></script>

<!-- 点击事件 -->
<script>
    $(function () {
        // 点击切换
        $(".tab-bar .tab-item").click(function () {
            var index = $(this).index();
            $(this).addClass("active").siblings().removeClass("active");
            $(".content .tab-content").eq(index).addClass("active").siblings().removeClass("active");
        })
    })
</script>

<!-- 新增同事 -->
<script>
    var edit_row = 0;
    var edit_col = 0;
    var row_id = 0;
    var col_name = 0;

    $(document).ready(function () {
        $("#staffList").jqGrid({
            url: '/system/authority/getUser.html?ajax=100',
            datatype: "json",//数据来源，本地数据（local，json,jsonp,xml等）
            height: $(window).height() * 0.75,//高度，表格高度。可为数值、百分比或'auto'
            colNames: ['操作','登录名称', '真实姓名', '手机号', '角色', '是否启用', '角色id'],
            colModel: [
                {name:'id', width:75, align:'center', formatter: cLink},
                { name: 'username', width: 215, label: '登录名称' },
                { name: 'name', width: 215, label: '真实姓名' },
                { name: 'telephone', width: 210, label: '手机号', align: "center" },
                {
                    name: "role_name",
                    width: 215,
                    label: "角色",
                    link: "inventory",
                    editable: true,
                    edittype: "custom",
                    editoptions: {
                        custom_element: function (t, e) {
                            $("#p_role_id").html('<div class="pr" id="role_id"><input type="text" value="' + t + '" class="textbox storageAuto customelement ui-combo-active" name="locationName" autocomplete="off" id="1_locationName" style="outline: -webkit-focus-ring-color auto 5px;"><span class="ui-icon-triangle-1-s" id="st_id"></span></div>');
                            return $("#role_id");
                        },
                        custom_value: function (t, e, a) {
                            var ba = $("#role_name").val();
                            $("#role_name").val("");
                            return ba;
                        },

                        trigger: "ui-icon-triangle-1-s"
                    }
                },
                { name: 'group_status', width: 150, label: '是否启用', align: "center", formatter: cSwitch },
                { name: 'role_id', label: '角色id', hidden: true },
            ],
            cellEdit: true,
            cellurl: '/',
            altRows: !0,
            gridview: !0,
            shrinkToFit: !1,
            cellLayout: 8,
            autowidth: !0,
            viewrecords: !0,
            cmTemplate: {
                sortable: !1,
                title: !1
            },
            loadError: function (t, e, i) {
                parent.Public.tips({
                    type: 1,
                    content: "操作失败了哦，请检查您的网络链接！"
                })
            },
            beforeEditCell: function (rowid, cellname, value, iRow, iCol) {
                col_name = cellname;
                edit_row = iRow;
                edit_col = iCol;
                row_id = rowid;
                var gname = $("#staffList").jqGrid("getCell", row_id, 'role_name');
                $("#role_name").val(gname);
            },
        });
    });

    function cLink(cellvalue, options, rowObject){
        return '<div class="operating"><span class="ui-icon ui-icon-pencil" onclick="lopen(\'edit.html?id='+rowObject.id+'\',\'编辑\',\'400px\',\'360px\', \'updateUser.html\')">修改</span> <span class="ui-icon ui-icon-trash" onclick="del('+rowObject.id+')">删除</span></div>';
    }

    function cSwitch(cellvalue, options, rowObject) {
        if (rowObject.status == 0) {
            return '<span class="set-status open" data-delete="false" onclick="switchs(this,' + rowObject.id + ')"><span class="ui-icon-circle"></span></span>';
        } else {
            return '<span class="set-status close" data-delete="false" onclick="switchs(this,' + rowObject.id + ')"><span class="ui-icon-circle"></span></span>';
        }
    }

    // 是否启用
    function switchs(obj, id) {
        if ($(obj).hasClass("open")) {
            $(obj).removeClass("open");
            $(obj).addClass("close");
            status = 1;
        } else {
            $(obj).removeClass("close");
            $(obj).addClass("open");
            status = 0;
        }
        $.ajax({
            type: "post",
            url: "updateUser",
            dataType: "json",
            data: {
                id: id,
                status: status

            },
            success: function (data) {
                if (data.status == 1) {
                    layer.msg(data.msg, { icon: 1, time: 1000 });
                } else {
                    layer.msg(data.msg, { icon: 2, time: 1000 });
                }
            },
            error: function (da) {
                layer.msg('请求失败');
            }
        })
        console.log(id);
    }

    $(document).ready(function () {
        // 选择角色弹窗
        $("#staffList").on("click", ".ui-icon-triangle-1-s", function (e) {
            var top = $(this).offset().top;
            var left = $(this).offset().left;
            $("#role_wrap").slideToggle();
            $("#role_wrap").css({ "top": (top + 22) + "px", "left": (left - 192) + "px" });
        })
        //选择角色赋值
        $("#role_wrap #role_list").on("click", ".list-item", function (e) {
            var title = $(this).attr("title");
            var role_id = $(this).attr("role_id");
            console.log(row_id, role_id);
            $.post('updateUser.html', { id: row_id, role_id: role_id }, function (data) {
                layer.msg(data.msg, { icon: 1, time: 1000 });
            })


            $("#role_name").val(title);
            $("#staffList").jqGrid("setCell", row_id, 'role_id', role_id);
            $("#staffList").jqGrid("setCell", row_id, 'role_name', title);//及时保存
            $("#staffList").jqGrid("saveCell", edit_row, edit_col);//保存数据表格
            $("#staffList").saveRow(row_id, false);
            $("#role_wrap").hide();
        });
    });
    //角色选择框外点击则隐藏
    $(document).mouseup(function (e) {
        // 组合件
        if (e.target.id != "st_id") {
            var con = $("#role_wrap");   // 设置目标区域
            if (!con.is(e.target) && con.has(e.target).length === 0) {
                $("#role_wrap").hide();
            }
        }
    });
</script>

<!-- 数据授权 -->
<script>
    $(document).ready(function () {
        var dataLength = 1;
        // 新增同事
        $("#grid").jqGrid({
            url: '/system/authority/index.html?ajax=100&sq=1',
            datatype: "json",//数据来源，本地数据（local，json,jsonp,xml等）
            height: $(window).height() * 0.75,//高度，表格高度。可为数值、百分比或'auto'
            colNames: ['成员(' + dataLength + ')', '真实姓名', '仓库', '客户', '供应商', '制单人', '商品', '账户'],
            colModel: [
                { name: '', width: 215, label: '成员' },
                { name: '', width: 215, label: '真实姓名' },
                { name: '', width: 100, label: '仓库' },
                { name: '', width: 100, label: '客户' },
                { name: '', width: 100, label: '供应商' },
                { name: '', width: 100, label: '制单人' },
                { name: '', width: 100, label: '商品' },
                { name: '', width: 100, label: '账户' },
            ],
            altRows: !0,
            gridview: !0,
            shrinkToFit: !1,
            cellLayout: 8,
            autowidth: !0,
            viewrecords: !0,
            cmTemplate: {
                sortable: !1,
                title: !1
            },
            loadError: function (t, e, i) {
                parent.Public.tips({
                    type: 1,
                    content: "操作失败了哦，请检查您的网络链接！"
                })
            }
        });
    });
</script>

<!-- 角色管理 -->
<script>
    $(document).ready(function () {

        $('#authority_tt').tree({
            url: 'getRoleTree.html',
            // data: [
            //     {
            //         id: 1,
            //         text: '老板',
            //         children: [
            //             { id: 1, text: '老板' }
            //         ],
            //     },
            //     {
            //         id: 2,
            //         text: '采购',
            //         children: [
            //             { text: '采购经理' },
            //             { text: '采购员' },
            //         ],
            //     },
            //     {
            //         id: 3,
            //         text: '销售',
            //         children: [
            //             { text: '销售员' },
            //         ],
            //     },
            //     {
            //         id: 4,
            //         text: '财务',
            //         children: [
            //             { text: '会计' },
            //             { text: '出纳' },
            //         ],
            //     },
            //     {
            //         id: 5,
            //         text: '仓管',
            //         children: [
            //             { text: '仓管员' },
            //         ],
            //     },
            //     {
            //         id: 6,
            //         text: '收银',
            //         children: [
            //             { text: '收银员' },
            //         ],
            //     },
            //     {
            //         id: 7,
            //         text: '市场',
            //         children: [
            //             { text: '市场专员' },
            //         ],
            //     },
            // ],
            formatter: function (node) {
                // console.log(node);
                if (node.pid == 0) {
                    return node.text + '<span class="button add hoverAdd" id="addBtn' + node.domId + '" title="新增" onclick=\'addNode("' + node.domId + '",' + node.id + ')\'></span>';
                } else {
                    return '<span class="textTitle" id="textTitle' + node.domId + '" >' + node.text + '</span><input class="textInputTitle" value="' + node.text + '" id="textTitleInput' + node.domId + '" /><span class="button copy hoverAdd" id="copyBtn' + node.domId + '" title="复制" onclick=\'copyNode("' + node.domId + '",' + node.id + ')\'></span> <span class="button edit hoverAdd" id="editBtn' + node.domId + '" title="修改" treenode_edit="" style="" onclick=\'editNode("' + node.domId + '")\'></span> <span class="button remove hoverAdd" id="removeBtn' + node.domId + '" title="删除" treenode_remove="" style="" onclick=\'delNode("' + node.domId + '",' + node.id + ')\'></span>';
                }

            },
            lines: true,
            animate: true,
            // 单击事件
            onClick: function (node) {

                // console.log(node);
                layui.use('form', function () {
                    var form = layui.form;

                    $("#rights-wrap :checkbox").prop("checked", false);
                    form.render('checkbox');

                    $("#rights-wrap :checkbox[name='name']").parents(".rights-content").addClass("hide");


                    if (node.pid == 0) {
                        console.log("点击父级");
                        return false;
                    } else {
                        // console.log("点击子级");
                        $.ajax({
                            type: "post",
                            url: "/system/authority/getAuth.html?id=" + node.id,
                            datatype: "json",
                            success: function (data) {
                                // console.log(data);
                                if (data.status == 1) {

                                    var grandpaLength = []; //二级菜单长度数组
                                    var grandpa = []; //二级菜单勾选数组
                                    var grandpaQ = []; //二级菜单勾选数组

                                    // layui.use('form', function () {
                                    //     var form = layui.form;

                                    for (var k = 0; k < data.list.length; k++) {
                                        // console.log(data.list[k].count);

                                        grandpaLength.push(data.list[k].count);
                                        grandpa.push(0);
                                        grandpaQ.push(0);

                                        for (var i = 0; i < data.list[k].son.length; i++) {
                                            // console.log(data.list[k].son[i].menu_id);
                                            // 判断勾选菜单复选框
                                            if (data.list[k].son[i].power_ids.length == 9) {
                                                $("#rights-wrap :checkbox[name='name'][data-pid='" + data.list[k].son[i].menu_id + "']").prop("checked", true);
                                                grandpa[k]++;
                                            } 
                                            // else {
                                            //     $("#rights-wrap :checkbox[name='name'][data-pid='" + data.list[k].son[i].menu_id + "']").prop("checked", false);
                                            // }

                                            // 循环勾选权限复选框
                                            for (var j = 0; j < data.list[k].son[i].power_ids.length; j++) {
                                                // console.log(data.list[k].son[i].power_ids[j]);
                                                $("#rights-wrap :checkbox[name!='name'][data-id='" + data.list[k].son[i].power_ids[j] + "'][data-pid='" + data.list[k].son[i].menu_id + "']").prop("checked", true);
                                                
                                                // 展开有权限的列表
                                                $("#rights-wrap :checkbox[name='name'][data-pid='" + data.list[k].son[i].menu_id + "']").parents(".rights-model").find(".rights-content").removeClass("hide");
                                            }
                                        }

                                        // 判断勾选一级菜单
                                        if (grandpa[k] >= grandpaLength[k]) {
                                            $("#rights-wrap :checkbox[name='name'][data-paid='" + data.list[k].id + "']").prop("checked", true);
                                        }
                                        // 判断勾选一级菜单
                                        if (grandpaQ[k] >= grandpaLength[k]) {
                                            // $("#rights-wrap :checkbox[name='query'][data-paid='"+data.list[k].id+"']").prop("checked", true);
                                        }
                                    }

                                    if(data.auth_show != ''){
                                        for(var i = 0; i < data.auth_show.length; i++){
                                            $("#rights-wrap :checkbox[data-text='one'][data-ids='" + data.auth_show[i] + "']").prop("checked", true);
                                        }
                                    }
                                    

                                    form.render('checkbox');
                                    // });

                                } else {
                                    layer.msg(data.msg);
                                }
                            },
                            error: function (data) {
                                layer.msg('请求失败');
                            }
                        });
                    }


                });


            },
            // 编辑完成
            onAfterEdit: function (node) {
                // console.log(node);
                var id = node.id;
                var name = node.text;

                console.log(id);
                console.log(name);

                $.ajax({
                    type: "post",
                    url: "editrole.html",
                    datatype: "json",
                    data: {
                        id: id,
                        name: name
                    },
                    success: function (data) {
                        console.log(data)
                        if (data.status == 1) {
                            // layer.msg('编辑成功');
                        } else {
                            layer.msg(data.msg);
                        }
                    },
                    error: function (data) {
                        layer.msg('请求失败');
                    }
                })
            }
        });


        // 保存按钮
        $("#saveRight").on("click", function () {
            // 获取所有根节点
            // var nodes = $('#authority_tt').tree('getRoots');
            // console.log(nodes);
            // 获取选中的id
            var id_array = [];
            var pid_array = [];
            var all_array = [];
            var SelectId;

            var selected = $('#authority_tt').tree('getSelected');  //当前节点
            // var SelectId = selected.id;

            // 获取一级菜单下对应的全选id
            $('#rights-wrap input[data-text=one]:checkbox:checked').each(function () {
                all_array.push($(this).attr('data-ids'));
            });

            // 获取二级菜单及对应权限的id
            $('.rights-content input[name!=name]:checkbox:checked').each(function () {
                id_array.push($(this).attr('data-id'));//向数组中添加元素
                pid_array.push($(this).attr('data-pid'));
            });

            // 判断是否选中角色
            // console.log(selected.pid);
            if(selected == null || selected == "" || selected == undefined){
                layer.msg("您未选择对应角色！");
                return false;
            }else{
                if(selected.pid==0){
                    layer.msg("操作失败，子角色不存在！");
                    return false;
                }else{
                    SelectId = selected.id;  //当前选中的角色id

                    $.post('save.html', { ids: id_array, pids: pid_array, role_id: SelectId, role_all:all_array }, function (res) {
                        if (res.status == 1) {
                            layer.msg(res.msg);
                        } else {
                            layer.msg(res.msg);
                        }
                    });
                }
            }
            
        })

    });

    // 新增(左侧菜单)
    function addNode(domId, id) {
        var domId = domId;
        var id = id;
        var selected = $('#authority_tt').tree('getSelected');

        var SelectId = selected.domId;

        if (domId == SelectId) {

            $.ajax({
                type: "post",
                url: "addrole.html",
                datatype: "json",
                data: {
                    pid: id,
                    name: 'undefined'
                },
                success: function (data) {
                    // console.log(data);
                    if (data.status == 1) {
                        $('#authority_tt').tree('append', {
                            parent: selected.target,  //父级
                            data: [{
                                id: data.id,
                                text: 'undefined'  //新增的文字
                            }]
                        });
                    } else {
                        layer.msg(data.msg);
                    }
                },
                error: function (data) {
                    layer.msg('请求失败');
                }
            });
        }

    }

    // 复制(左侧菜单)
    function copyNode(domId, id) {
        var domId = domId;
        var selected = $('#authority_tt').tree('getSelected');  //当前节点
        var SelectId = selected.domId;

        var parent = $('#authority_tt').tree('getParent', $('#' + selected.domId)[0]);//父节点

        var pid = parent.id; //父级的id


        var num = '';  //随机数
        for (var i = 0; i < 3; i++) {
            num += Math.floor(Math.random() * 10);
        }
        var copyText = parent.text + '' + num;  //父级节点的文字+随机数

        if (domId == SelectId) {

            var content = '<div class="tipBox"><div class="leftImg"><img src="/public/static/admin/css/img/confirm.gif" style="width:100%;height:100%;"/></div><div class="rightText">此操作将复制该角色的所有功能权限，请确认是否复制？</div></div>';
            layer.open({
                type: 1,
                title: "系统提示",
                area: ['280px', 'auto'], //宽高
                content: content,
                btn: ['确定', '取消'],
                btn1: function (index, layero) {

                    $.ajax({
                        type: "post",
                        url: "copyrole.html",
                        datatype: "json",
                        data: {
                            pid: pid,
                            id: id,
                            newname: copyText
                        },
                        success: function (data) {
                            // console.log(data)
                            if (data.status == 1) {
                                // layer.msg('编辑成功');

                                // 在父级节点的最后添加
                                $('#authority_tt').tree('append', {
                                    parent: parent.target,
                                    data: {
                                        id: data.id,
                                        text: copyText,
                                    }
                                });

                            } else {
                                layer.msg(data.msg);
                            }
                        },
                        error: function (data) {
                            layer.msg('请求失败');
                        }
                    });
                    layer.close(index);
                },
                btn2: function (index, layero) {
                    layer.close(index);
                    return false;
                }
            });




        }
    }

    // 编辑(左侧菜单)
    function editNode(domId) {
        var domId = domId;
        var selected = $('#authority_tt').tree('getSelected');

        var SelectId = selected.domId;

        if (domId == SelectId) {

            // 设置可编辑
            $("#authority_tt").tree('beginEdit', selected.target);
        }
    }

    // 删除(左侧菜单)
    function delNode(domId, id) {
        var domId = domId;
        var selected = $('#authority_tt').tree('getSelected');
        var SelectId = selected.domId;

        if (domId == SelectId) {
            var content = '<div class="tipBox"><div class="leftImg"><img src="/public/static/admin/css/img/confirm.gif" style="width:100%;height:100%;"/></div><div class="rightText">删除的角色将不能恢复，请确认是否删除？</div></div>';
            layer.open({
                type: 1,
                title: "系统提示",
                area: ['280px', 'auto'], //宽高
                content: content,
                btn: ['确定', '取消'],
                btn1: function (index, layero) {

                    $.ajax({
                        type: "post",
                        url: "delrole.html",
                        datatype: "json",
                        data: {
                            id: id,
                        },
                        success: function (data) {
                            // console.log(data)
                            if (data.status == 1) {
                                $('#authority_tt').tree("remove", selected.target);  //移除节点
                                layer.msg(data.msg);
                            } else {
                                layer.msg(data.msg);
                            }
                        },
                        error: function (data) {
                            layer.msg('请求失败');
                        }
                    });

                    layer.close(index);
                },
                btn2: function (index, layero) {
                    layer.close(index);
                    return false;
                }
            });
        }
    }


    $(document).ready(function () {
        // 右侧列表（展开、收起）
        $(".fold-btn.fl.fold-close").on("click", function () {

            if ($(this).hasClass('fold-open')) {
                // 判断改变图标
                $(this).addClass("fold-close").removeClass("fold-open");
                // 收起列表
                $(this).parents(".rights-title").siblings(".rights-content").addClass("hide");
            } else {
                $(this).addClass("fold-open").removeClass("fold-close");
                $(this).parents(".rights-title").siblings(".rights-content").removeClass("hide");
            }
        });


        // 复选框事件
        layui.use('form', function () {
            var form = layui.form;

            //全选
            form.on('checkbox(allChoose)', function (data) {
                var a = data.elem.checked;
                if (a == true) {
                    $("#rights-wrap :checkbox").prop("checked", true);
                    form.render('checkbox');
                } else {
                    $("#rights-wrap :checkbox").prop("checked", false);
                    form.render('checkbox');
                }
            });


            // （一级分类）标题全选（监听其父级下的checkbox选中状态）
            form.on('checkbox(childAllChoose)', function (data) {
                var a = data.elem.checked;

                if (a == true) {
                    $(this).parents(".rights-model").find(":checkbox").prop("checked", true);
                    form.render('checkbox');
                } else {
                    $(this).parents(".rights-model").find(":checkbox").prop("checked", false);
                    $("#allChoose").prop("checked", false);
                    form.render('checkbox');
                }

                // 验证全部按钮反全选（如果一级菜单勾选完，则勾选全部按钮）
                var item = $(".rights-model .rights-title :checkbox[name=name]");  //获取一级菜单名称input
                var allOne = item.length;
                for (var i = 0; i < item.length; i++) {
                    if (item[i].checked == true) {
                        allOne--;
                    }
                }
                if (allOne == 0) {
                    $("#allChoose").prop("checked", true);
                    form.render('checkbox');
                }

            });


            // （二级分类）标题全选
            form.on('checkbox(grandsonAllChoose)', function (data) {
                var a = data.elem.checked;

                if (a == true) {
                    $(this).parents(".rights-content").find(":checkbox").prop("checked", true);
                    form.render('checkbox');
                } else {
                    $(this).parents(".rights-content").find(":checkbox").prop("checked", false); //取消菜单下所有
                    $("#allChoose").prop("checked", false);  //取消全选
                    $(this).parents(".rights-model").find(".rights-title :checkbox[name=name]").prop("checked", false);  //取消一级菜单名称
                    form.render('checkbox');
                }


                // 反选一级菜单
                var itemTow = $(this).parents(".rights-model").find(".rights-content :checkbox[name=name]");  //获取二级级菜单名称input
                var allTow = itemTow.length;
                for (var i = 0; i < itemTow.length; i++) {
                    if (itemTow[i].checked == true) {
                        allTow--;
                    }
                }
                if (allTow == 0) {
                    // 验证一级菜单反全选（如果二级菜单勾选完，则勾选对应一级菜单）
                    $(this).parents(".rights-model").find(".rights-title :checkbox[name=name]").prop("checked", true);
                    form.render('checkbox');

                    // 验证全部按钮反全选（如果一级菜单勾选完，则勾选全部按钮）
                    var itemOne = $(".rights-model .rights-title :checkbox[name=name]");  //获取一级菜单名称input
                    var allOne = itemOne.length;
                    for (var i = 0; i < itemOne.length; i++) {
                        if (itemOne[i].checked == true) {
                            allOne--;
                        }
                    }
                    if (allOne == 0) {
                        $("#allChoose").prop("checked", true);
                        // $("#rights-wrap :checkbox").prop("checked", true);
                        form.render('checkbox');
                    }

                }


            });


            // 点击全部--类型全选
            form.on('checkbox(typeAllChoose)', function (data) {
                var a = data.elem.checked;
                var type = data.elem.dataset.type;
                if (a == true) {
                    $("#rights-wrap :checkbox[name=" + type + "]").prop("checked", true);
                    form.render('checkbox');
                } else {
                    // 判断如果是去选查询全选
                    if (type == "query") {
                        var content = '<div class="tipBox"><div class="leftImg"><img src="/public/static/admin/css/img/confirm.gif" style="width:100%;height:100%;"/></div><div class="rightText">取消查看权限将同时取消其他所有权限，请确认是否取消？</div></div>';
                        layer.open({
                            type: 1,
                            title: "系统提示",
                            area: ['280px', 'auto'], //宽高
                            content: content,
                            btn: ['确定', '取消'],
                            btn1: function (index, layero) {
                                $("#rights-wrap :checkbox").prop("checked", false);
                                form.render('checkbox');
                                layer.close(index);
                            },
                            btn2: function (index, layero) {
                                $("#queryAllChoose").prop("checked", true);
                                form.render('checkbox');
                                layer.close(index);
                                return false;
                            }
                        });

                    } else {
                        $("#rights-wrap :checkbox[name=" + type + "]").prop("checked", false);  //取消所有对应类型的全选
                        $("#allChoose").prop("checked", false);  //取消全选
                        $("#rights-wrap :checkbox[name=name]").prop("checked", false);  //取消标题名称全选
                        form.render('checkbox');
                    }
                }


                // 验证全部按钮反全选（如果类型菜单勾选完，则勾选全部按钮）
                var item = $(".rights-header .rights-title :checkbox[name!=name]");  //获取一级菜单名称input
                var allOne = item.length;
                for (var i = 0; i < item.length; i++) {
                    if (item[i].checked == true) {
                        allOne--;
                    }
                }
                if (allOne == 0) {
                    $("#allChoose").prop("checked", true);
                    $("#rights-wrap :checkbox").prop("checked", true);
                    form.render('checkbox');


                }

            });


            // 点击一级菜单下的类型全选
            form.on('checkbox(childAll)', function (data) {
                var a = data.elem.checked;
                var type = data.elem.dataset.type;
                if (a == true) {
                    $(this).parents(".rights-model").find(":checkbox[name=" + type + "]").prop("checked", true);
                    form.render('checkbox');
                } else {
                    $(this).parents(".rights-model").find(":checkbox[name=" + type + "]").prop("checked", false);  //取消子元素所有对应type
                    $("#allChoose").prop("checked", false);  //取消全选
                    $("#" + type + "AllChoose").prop("checked", false);  //取消全选(全部--对应type)
                    $(this).parents(".rights-model").find(":checkbox[name=name]").prop("checked", false);  //父级标题取消全选
                    form.render('checkbox');
                }


                // 验证全部按钮反全选（如果类型菜单勾选完，则勾选全部按钮）
                var typeItemAll = $(this).parents(".rights-title").find(":checkbox[name!=name]");
                var typeAll = typeItemAll.length;
                for (var i = 0; i < typeItemAll.length; i++) {
                    if (typeItemAll[i].checked == true) {
                        typeAll--;
                    }
                }
                if (typeAll == 0) {
                    // $("#allChoose").prop("checked", true);
                    $(this).parents(".rights-model").find(":checkbox[name=name]").prop("checked", true);
                    form.render('checkbox');


                    // 验证全部按钮反全选（如果一级菜单勾选完，则勾选全部按钮）
                    var itemOne = $(".rights-model .rights-title :checkbox[name=name]");  //获取一级菜单名称input
                    var allOne = itemOne.length;
                    for (var i = 0; i < itemOne.length; i++) {
                        if (itemOne[i].checked == true) {
                            allOne--;
                        }
                    }
                    if (allOne == 0) {
                        $("#allChoose").prop("checked", true);
                        // $("#rights-wrap :checkbox").prop("checked", true);
                        form.render('checkbox');
                    }

                }

                // 验证顶部对应类型全选(获取所有对应类型的按钮)
                var typeItems = $(".rights-model .rights-title :checkbox[name=" + type + "]");
                var alltypeItem = typeItems.length;
                for (var i = 0; i < typeItems.length; i++) {
                    if (typeItems[i].checked == true) {
                        alltypeItem--;
                    }
                }
                if (alltypeItem == 0) {
                    $(".rights-header .rights-title :checkbox[name=" + type + "]").prop("checked", true);
                    form.render('checkbox');
                }


            });


            // 点击单个item
            form.on('checkbox(itemBox)', function (data) {
                var a = data.elem.checked;
                var type = data.elem.dataset.type;
                if (a == true) {
                    // $(this).parents(".rights-model").find(":checkbox[name=others]").prop("checked", true);
                    // form.render('checkbox');
                } else {
                    $("#allChoose").prop("checked", false);  //取消全选
                    $("#" + type + "AllChoose").prop("checked", false);  //取消全选(全部--对应的type)
                    $(this).parents(".rights-model").find(".rights-title :checkbox[name=" + type + "]").prop("checked", false);  //父级标题（对应type）取消全选
                    $(this).parents(".rights-model").find(".rights-title :checkbox[name=name]").prop("checked", false);  //父级标题（一级）取消全选
                    $(this).parents(".rights-content").find(":checkbox[name=name]").prop("checked", false);  //父级标题(二级)取消全选
                    form.render('checkbox');
                }


                // 判断二级菜单是否全选(如果全选，则勾选二级菜单标题)
                var towItem = $(this).parents(".rights-content").find(":checkbox[name!=name]");
                var childAllTow = towItem.length;
                for (var i = 0; i < towItem.length; i++) {
                    if (towItem[i].checked == true) {
                        childAllTow--;
                    }
                }
                if (childAllTow == 0) {
                    $(this).parents(".rights-content").find(":checkbox[name=name]").prop("checked", true);
                    form.render('checkbox');
                }

                // 判断子类型全选
                var typeItemTow = $(this).parents(".rights-model").find(".rights-content :checkbox[name=" + type + "]");
                var childAllTypeTow = typeItemTow.length;
                for (var i = 0; i < typeItemTow.length; i++) {
                    if (typeItemTow[i].checked == true) {
                        childAllTypeTow--;
                    }
                }
                if (childAllTypeTow == 0) {
                    $(this).parents(".rights-model").find(".rights-title :checkbox[name=" + type + "]").prop("checked", true);
                    form.render('checkbox');

                    // 验证顶部对应类型全选(获取所有对应类型的按钮)
                    var typeItems = $(".rights-model .rights-title :checkbox[name=" + type + "]");
                    var alltypeItem = typeItems.length;
                    for (var i = 0; i < typeItems.length; i++) {
                        if (typeItems[i].checked == true) {
                            alltypeItem--;
                        }
                    }
                    if (alltypeItem == 0) {
                        $(".rights-header .rights-title :checkbox[name=" + type + "]").prop("checked", true);
                        form.render('checkbox');
                    }
                }

                // 判断一级菜单是否全选(如果全选，则勾选一级菜单标题)
                var oneItem = $(this).parents(".rights-model").find(".rights-content :checkbox[name!=name]");
                var childAllOne = oneItem.length;
                for (var i = 0; i < oneItem.length; i++) {
                    if (oneItem[i].checked == true) {
                        childAllOne--;
                    }
                }
                if (childAllOne == 0) {
                    $(this).parents(".rights-model").find(":checkbox").prop("checked", true);
                    form.render('checkbox');

                    // 验证全部按钮反全选（如果一级菜单勾选完，则勾选全部按钮）
                    var item = $(".rights-model .rights-title :checkbox[name=name]");  //获取一级菜单名称input
                    var allOne = item.length;
                    for (var i = 0; i < item.length; i++) {
                        if (item[i].checked == true) {
                            allOne--;
                        }
                    }
                    if (allOne == 0) {
                        $("#allChoose").prop("checked", true);
                        $(".rights-header .rights-title :checkbox").prop("checked", true);
                        form.render('checkbox');
                    }
                }


            });
        });
    });


</script>
{include file="admin@public/footer"/}